package com.todo.sight.application.convert;


import com.google.api.client.util.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.CityTypeEnum;
import com.todo.sight.application.entity.condition.CityInfoConditionBO;
import com.todo.sight.application.iface.backend.request.InsertCityInfoReq;
import com.todo.sight.application.iface.backend.request.QueryCityInfoByConditionReq;
import com.todo.sight.application.iface.backend.request.UpdateCityInfoReq;
import com.todo.sight.application.iface.backend.response.CityInfoDetailResp;
import com.todo.sight.application.iface.backend.response.QueryCityInfoByConditionResp;
import com.todo.sight.application.entity.bo.CityInfoBO;
import com.todo.sight.application.entity.condition.CityInfoConditionBO;
import com.todo.sight.application.iface.backend.request.InsertCityInfoReq;
import com.todo.sight.application.iface.backend.request.QueryCityInfoByConditionReq;
import com.todo.sight.application.iface.backend.request.UpdateCityInfoReq;
import com.todo.sight.application.iface.backend.response.CityInfoDetailResp;
import com.todo.sight.application.iface.backend.response.QueryCityInfoByConditionResp;
import com.todo.sight.application.iface.frontend.response.CityDetailVO;
import com.todo.sight.application.iface.frontend.response.CityInfoVO;
import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/5/16 15:21
 * @description
 */
public class CityInfoConvert {

    public static CityInfoBO insertCityInfoReqconverToCityInfoBO(InsertCityInfoReq req) {
        if (req == null) {
            return null;
        }
        CityInfoBO cityInfoBO = new CityInfoBO();
        cityInfoBO.setName(req.getName());
        cityInfoBO.setType(CityTypeEnum.getByType(req.getType()));
        cityInfoBO.setParentId(req.getParentId());
        cityInfoBO.setOtherName(req.getOtherName());
        cityInfoBO.setMainImg(req.getMainImg());
        cityInfoBO.setDesc(req.getDesc());
        cityInfoBO.setIsShow(req.getIsShow());
        cityInfoBO.setLon(new BigDecimal(req.getLon()));
        cityInfoBO.setLat(new BigDecimal(req.getLat()));
        return cityInfoBO;

    }

    public static CityInfoBO updateCityInfoReqconverToCityInfoBO(UpdateCityInfoReq req) {
        if (req == null) {
            return null;
        }
        CityInfoBO cityInfoBO = new CityInfoBO();
        cityInfoBO.setId(req.getId());
        cityInfoBO.setName(req.getName());
        cityInfoBO.setType(CityTypeEnum.getByType(req.getType()));
        cityInfoBO.setParentId(req.getParentId());
        cityInfoBO.setOtherName(req.getOtherName());
        cityInfoBO.setMainImg(req.getMainImg());
        cityInfoBO.setDesc(req.getDesc());
        cityInfoBO.setIsShow(req.getIsShow());
        return cityInfoBO;
    }

    public static CityInfoDetailResp convertToCityInfoDetailResp(CityInfoBO cityInfoBO) {
        if (cityInfoBO == null) {
            return null;
        }
        CityInfoDetailResp cityInfoDetailResp = new CityInfoDetailResp();
        cityInfoDetailResp.setId(cityInfoBO.getId());
        cityInfoDetailResp.setName(cityInfoBO.getName());
        cityInfoDetailResp.setType(cityInfoBO.getType().getType());
        cityInfoDetailResp.setParentId(cityInfoBO.getParentId());
        cityInfoDetailResp.setOtherName(cityInfoBO.getOtherName());
        cityInfoDetailResp.setMainImg(cityInfoBO.getMainImg());
        cityInfoDetailResp.setDesc(cityInfoBO.getDesc());
        cityInfoDetailResp.setIsShow(cityInfoBO.getIsShow());
        cityInfoDetailResp.setLon(String.valueOf(cityInfoBO.getLon()));
        cityInfoDetailResp.setLat(String.valueOf(cityInfoBO.getLat()));
        return cityInfoDetailResp;
    }

    public static CityInfoConditionBO convertToCityInfoConditionBO(QueryCityInfoByConditionReq req) {
        if (req == null) {
            return null;
        }
        CityInfoConditionBO conditionBO = new CityInfoConditionBO();
        conditionBO.setPageSize(req.getPageSize());
        conditionBO.setPageIndex(req.getPageIndex());
        conditionBO.setCurrentIndex(req.getCurrentIndex());
        conditionBO.setName(req.getName());
        conditionBO.setType(CityTypeEnum.getByType(req.getType()));
        conditionBO.setIsShow(req.getIsShow());
        conditionBO.setCityId(req.getId());
        return conditionBO;
    }

    public static PageResult<QueryCityInfoByConditionResp> convertToQueryCityInfoByConditionRespList(PageResult<CityInfoBO> result) {
        if (result == null) {
            return null;
        }
        if (result.getResults() == null) {
            return PageResult.of(null, result.getPageNum(), result.getPageSize(), result.getTotal());
        }
        return PageResult.of(result.getResults().stream().map(CityInfoConvert::convertToQueryCityInfoByConditionResp).collect(Collectors.toList()), result.getPageNum(), result.getPageSize(), result.getTotal());
    }

    private static QueryCityInfoByConditionResp convertToQueryCityInfoByConditionResp(CityInfoBO cityInfoBO) {
        if (cityInfoBO == null) {
            return null;
        }
        QueryCityInfoByConditionResp queryCityInfoByConditionResp = new QueryCityInfoByConditionResp();
        queryCityInfoByConditionResp.setId(cityInfoBO.getId());
        queryCityInfoByConditionResp.setName(cityInfoBO.getName());
        queryCityInfoByConditionResp.setType(cityInfoBO.getType() == null ? null : cityInfoBO.getType().getType());
        queryCityInfoByConditionResp.setParentId(cityInfoBO.getParentId());
        queryCityInfoByConditionResp.setOtherName(cityInfoBO.getOtherName());
        queryCityInfoByConditionResp.setIsShow(cityInfoBO.getIsShow());
        queryCityInfoByConditionResp.setLon(String.valueOf(cityInfoBO.getLon()));
        queryCityInfoByConditionResp.setLat(String.valueOf(cityInfoBO.getLat()));
        return queryCityInfoByConditionResp;
    }

    public static List<CityInfoVO> convertToCityInfoVOList(List<CityInfoBO> cityInfoBOList) {
        if (CollectionUtils.isEmpty(cityInfoBOList)){
            return Lists.newArrayList();
        }
        return cityInfoBOList.stream().map(CityInfoConvert::convertToCityInfoVO).collect(Collectors.toList());
    }

    private static CityInfoVO convertToCityInfoVO(CityInfoBO cityInfoBO) {
        if (cityInfoBO == null){
            return null;
        }
        CityInfoVO cityInfoVO = new CityInfoVO();
        cityInfoVO.setCityId(cityInfoBO.getId());
        cityInfoVO.setName(cityInfoBO.getName());
        cityInfoVO.setTypeName(cityInfoBO.getType() == null ? null : cityInfoBO.getType().getName());
        cityInfoVO.setParentId(cityInfoBO.getParentId());
        cityInfoVO.setOtherName(cityInfoBO.getOtherName());
        cityInfoVO.setTypeCode(cityInfoBO.getType() == null ? null : cityInfoBO.getType().getCode());
        cityInfoVO.setMainImg(cityInfoBO.getMainImg());
        cityInfoVO.setLat(cityInfoBO.getLat());
        cityInfoVO.setLon(cityInfoBO.getLon());
        return cityInfoVO;
    }

    public static CityDetailVO convertToCityDetailVO(CityInfoBO cityInfoBO) {
        if (cityInfoBO == null){
            return null;
        }
        CityDetailVO cityDetailVO = new CityDetailVO();
        cityDetailVO.setCityId(cityInfoBO.getId());
        cityDetailVO.setName(cityInfoBO.getName());
        cityDetailVO.setTypeName(cityInfoBO.getType().getName());
        cityDetailVO.setParentId(cityInfoBO.getParentId());
        cityDetailVO.setOtherName(cityInfoBO.getOtherName());
        cityDetailVO.setTypeCode(cityInfoBO.getType().getCode());
        cityDetailVO.setMainImg(cityInfoBO.getMainImg());
        cityDetailVO.setDesc(cityInfoBO.getDesc());
        cityDetailVO.setLat(cityInfoBO.getLat());
        cityDetailVO.setLon(cityInfoBO.getLon());
        return cityDetailVO;
    }
}
